<%@ Page Language="C#"%>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="JSClass" %>
<%
    String layername = Request["layername"] == null ? "LEPS_RI" : Request["layername"];
	String start = Request["start"] == null ? "1" : Request["start"];
    String end = Request["end"] == null ? "10" : Request["end"];
	
	StringBuilder sb = new StringBuilder();
    sb.Append("SELECT * \n");
    sb.Append("FROM \n");
    sb.Append("( \n");
	
	if(layername == "LEPS_SID")
	{
		sb.Append("SELECT \n");
		sb.Append("	ROWNUM RNUM , HJGYCODE,SDMYEONG,SGGMYEONG,EMDMYEONG,RMYEONG \n");
		sb.Append("FROM CODE_HJGY A  \n");
		sb.Append("WHERE SYYEOBU='1' \n");
		sb.Append("	AND SUBSTR(HJGYCODE,3,8) = '00000000' \n");
		sb.Append("	AND HJGYCODE NOT IN (SELECT NAME FROM LEPS_SID) \n");
	}
	else if(layername == "LEPS_SGG")
	{
		sb.Append("SELECT \n");
		sb.Append("	ROWNUM RNUM , HJGYCODE,SDMYEONG,SGGMYEONG,EMDMYEONG,RMYEONG \n");
		sb.Append("FROM CODE_HJGY A  \n");
		sb.Append("WHERE SYYEOBU='1' \n");
		sb.Append("	AND SUBSTR(HJGYCODE,3,3) <> '000' AND SUBSTR(HJGYCODE,6,5) = '00000' \n");
		sb.Append("	AND HJGYCODE NOT IN (SELECT NAME FROM LEPS_SGG) \n");
	}
	else if(layername == "LEPS_EMD")
	{
		sb.Append("SELECT \n");
		sb.Append("	ROWNUM RNUM , HJGYCODE,SDMYEONG,SGGMYEONG,EMDMYEONG,RMYEONG \n");
		sb.Append("FROM CODE_HJGY A  \n");
		sb.Append("WHERE SYYEOBU='1' \n");
		sb.Append("	AND SUBSTR(HJGYCODE,6,3) <> '000' AND SUBSTR(HJGYCODE,9,2) = '00' \n");
		sb.Append("	AND HJGYCODE NOT IN (SELECT NAME FROM LEPS_EMD) \n");
	}
	else if(layername == "LEPS_RI")
	{
		sb.Append("SELECT \n");
		sb.Append("	ROWNUM RNUM , HJGYCODE,SDMYEONG,SGGMYEONG,EMDMYEONG,RMYEONG \n");
		sb.Append("FROM CODE_HJGY A  \n");
		sb.Append("WHERE SYYEOBU='1' \n");
		sb.Append("	AND SUBSTR(HJGYCODE,9,2) <> '00' \n");
		sb.Append("	AND HJGYCODE NOT IN (SELECT NAME FROM LEPS_RI) \n");
	}
	
	sb.Append(") \n");
    sb.Append("WHERE RNUM >= " + start + " AND RNUM <= " + end + " \n");
	
	TableQuery myQuery = new TableQuery(sb.ToString());
    Response.Write(myQuery.GetJSON());
    %>